// 封装链表
function LinkedList() {
  // 内部类：节点类
  function Node(data) {
    this.data = data
    this.next = null
  }
  // 属性
  this.head = null
  this.length = 0

  // 追加方法
  LinkedList.prototype.append = function (data) {
    // 创建新节点
    var newNode = new Node(data)
    // 判断是否为第一个节点
    if (this.length == 0) {
      this.head = newNode
    } else {
      var current = this.head
      while (current.next) {
        current = current.next
      }
      current.next = newNode
    }
    this.length += 1
  }

  // toString方法
  LinkedList.prototype.toString = function () {
    var current = this.head
    var listString = ''
    while (current) {
      listString += current.data + ' '
      current = current.next
    }
    return listString
  }
  // 插入方法
  LinkedList.prototype.insert = function (position, data) {
    // 进行越界判断
    if (position < 0 || position > this.length) return false
    // 根据data创建newNode
    var newNode = new Node(data)
    if (position == 0) {
      newNode.next = this.head
      this.head = newNode
    } else {
      var index = 0
      var current = this.head
      var previous = null
      while (index++ < position) {
        previous = current
        current = current.next
      }
      newNode.next = current
      previous.next = newNode
    }
    this.length += 1
    return true
  }
  // get方法
  LinkedList.prototype.get = function (position) {
    if (position < 0 || position >= this.length) return null
    // 获取data
    var current = this.head
    var index = 0
    while (index < position) {
      current = current.next
      index += 1
    }
    return current.data
  }

  // indexOf(element)方法
  LinkedList.prototype.indexOf = function (data) {
    var current = this.head
    var index = 0
    while (current) {
      if (current.data == data) {
        return index
      }
      current = current.next
      index += 1
    }
    return -1
  }
  // update(position， element)方法
  LinkedList.prototype.update = function (position, newData) {
    if (position < 0 || position > this.length) return false
    var current = this.head
    var index = 0
    while (current < position) {
      current = current.next
    }
    current.data = newData
    return true
  }

  // removeAt(position)方法
  LinkedList.prototype.removeAt = function (position) {
    // 越界判断
    if (position < 0 || position > this.length) return null
    var current = this.head
    if (position == 0) {
      this.head = this.head.next
    } else {
      var index = 0
      var current = this.head
      var previous = null
      while (index++ < position) {
        previous = current
        current = current.next
      }
      previous.next = current.next
    }
    this.length -= 1
    return current.data
  }

  // remove方法
  LinkedList.prototype.remove = function (data) {
    var position = this.indexOf(data)
    return this.removeAt(position)
  }

  // isEmpty方法
  LinkedList.prototype.isEmpty = function () {
    return this.length == 0
  }
  // size方法
  LinkedList.prototype.size = function () {
    return this.length
  }
}
var list = new LinkedList()
list.append('abc1')
list.append('cba1')
list.append('nba1')
// alert(list)

// list.insert(0, 'aaa')
// list.insert(3, 'bbb')
// list.insert(5, 'ccc')
// alert(list)

// alert(list.get(0))
// alert(list.get(3))
// alert(list.get(4))

// alert(list.indexOf('ccc'))
// alert(list.indexOf('ggg'))

// alert(list.update(0, 'ttt'))
// alert(list)

// list.removeAt(0)
// list.remove('abc1')
// alert(list)

alert(list.isEmpty())
alert(list.size())
